import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.sql.*;
import java.util.Scanner;
public class Test1 {
    public static void main(String[] args) throws SQLException {
        MysqlDataSource ds=new MysqlDataSource();

        ((MysqlDataSource)ds).setUrl("jdbc:mysql://127.0.0.1:3306/school?useSSL=false");
        ((MysqlDataSource)ds).setUser("root");
        ((MysqlDataSource)ds).setPassword("123456");

        Connection conn=ds.getConnection();
        String sql="select * from stu where sex=?";
        PreparedStatement ps=conn.prepareStatement(sql);

        //1、直接设置
        //ps.setString(1,"男");

        //2、手动输入
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入你要查询的性别：");
        String n=scanner.next();
        ps.setString(1,n);

        ResultSet rs=ps.executeQuery();
        while (rs.next()){
            String id=rs.getString(1);
            String name=rs.getString(2);
            String sex=rs.getString(3);
            int age=rs.getInt(4);
            String dept=rs.getString(5);
            System.out.println("学号"+id+" 姓名："+name+" 性别："+sex+" 年龄："+age+" 专业："+dept);
        }

        rs.close();
        ps.close();
        conn.close();
    }
}
